/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.ververica.cdc.connectors.postgres;

import org.apache.flink.test.util.AbstractTestBase;

import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.lifecycle.Startables;

import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.Assert.assertNotNull;

/**
 * Basic class for testing PostgresSQL source, this contains a PostgreSQL container which enables binlog.
 */
public abstract class PostgresTestBase extends AbstractTestBase {
	private static final Logger LOG = LoggerFactory.getLogger(PostgresTestBase.class);
	private static final Pattern COMMENT_PATTERN = Pattern.compile("^(.*)--.*$");


	protected static final PostgreSQLContainer<?> POSTGERS_CONTAINER = new PostgreSQLContainer<>("debezium/postgres:9.6")
		.withDatabaseName("postgres")
		.withUsername("postgres")
		.withPassword("postgres")
		.withLogConsumer(new Slf4jLogConsumer(LOG));

	@BeforeClass
	public static void startContainers() {
		LOG.info("Starting containers...");
		Startables.deepStart(Stream.of(POSTGERS_CONTAINER)).join();
		LOG.info("Containers are started.");
	}

	protected Connection getJdbcConnection() throws SQLException {
		return DriverManager.getConnection(
			POSTGERS_CONTAINER.getJdbcUrl(),
			POSTGERS_CONTAINER.getUsername(),
			POSTGERS_CONTAINER.getPassword()
		);
	}

	/**
	 * Executes a JDBC statement using the default jdbc config without autocommitting the connection.
	 */
	protected void initializePostgresTable(String sqlFile) {
		final String ddlFile = String.format("ddl/%s.sql", sqlFile);
		final URL ddlTestFile = PostgresTestBase.class.getClassLoader().getResource(ddlFile);
		assertNotNull("Cannot locate " + ddlFile, ddlTestFile);
		try (Connection connection = getJdbcConnection();
				Statement statement = connection.createStatement()) {
			final List<String> statements = Arrays.stream(
				Files.readAllLines(Paths.get(ddlTestFile.toURI())).stream()
					.map(String::trim)
					.filter(x -> !x.startsWith("--") && !x.isEmpty())
					.map(x -> {
						final Matcher m = COMMENT_PATTERN.matcher(x);
						return m.matches() ? m.group(1) : x;
					})
					.collect(Collectors.joining("\n")).split(";"))
				.collect(Collectors.toList());
			for (String stmt : statements) {
				statement.execute(stmt);
			}
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
